package com.yunhai.common.utils;

import java.lang.reflect.Field;

public class ReflectionCopy {
    public static void copyProperties(Object dest, Object src) {
        Class<?> srcClass = src.getClass();
        Field[] fields = srcClass.getDeclaredFields(); // 获取所有字段，包括私有字段
        Field[] descFields = dest.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true); // 设置私有字段可访问
            for( Field descField : descFields ){
                descField.setAccessible(true);
                try {
                    System.out.println("descField.getName(): "+descField.getName());
                    System.out.println(" field.getName(): "+ field.getName());
                    if( descField.getName().equals( field.getName() ) ){
                        Object value =  field.get(src);
                        descField.set(dest, value);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}